#include "AnaDef.h"
#include "AliPlot.h"
#include "XGLUtils.h"

void plot(const Int_t itype)
{
  TCanvas *cc = new TCanvas("cc");
  AliPlot::PadSetup(cc, 0.12, 0.04, 0.12, 0.13);

  gPad->SetLogz();

  TH2D *htmp=(TH2D*)gDirectory->Get("hrELtrdQ0");
  TH2D *hh=htmp; //XGLUtils::NormalHist(htmp,100, kFALSE);
  AliPlot::ResetStyle(hh);

  hh->SetXTitle("p (GeV/c)");
  //hh->SetYTitle("TRD Truncated Mean / Expected #pi Signal");
  hh->SetYTitle("TRD dE/dx+TR (a.u.)");
  hh->SetMinimum(1.000001);
  hh->Draw("colz");

  /*
  TH1D *mpi=(TH1D*)gDirectory->Get("mpi");
  AliPlot::ResetStyle(mpi);
  TH1D *mel=(TH1D*)gDirectory->Get("mel");
  AliPlot::ResetStyle(mel);
  TH1D *mpr=(TH1D*)gDirectory->Get("mpr");
  AliPlot::ResetStyle(mpr);

  mpi->Draw("same C");
  mel->Draw("same C");
  mpr->Draw("same C");
  */

  TLatex *la = 0x0;
  if(itype==2)
    la = new TLatex(0.62, 0.9, "p, #pi, e (pp,#sqrt{s}=7 TeV)");
  else if(itype==4)
    la = new TLatex(0.32, 0.9, "p, #pi, e (PbPb,#sqrt{s}=2.76 ATeV, Centrality 0-20%)");

  AliPlot::ResetStyle(la, 0.8);
  la->Draw();

  AliPlot::DrawLogo(cc, 0.67, 0.65, 0.80, 0.85, "10/03/2012");
  
  cc->Print(Form("performance_2d_trd%d.eps",itype));
  cc->Print(Form("performance_2d_trd%d.png",itype));

  TFile *fout = new TFile(Form("outplotResolution%d.root", itype),"recreate");
  cc->Write();
  hh->Write();
  fout->Save();
  fout->Close();
}

int main(int argc, char * argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  AliPlot::SetGlobalStyle();
  const TString fin(argv[1]);
  TFile::Open(fin);
  Int_t itype = -999;
  if(fin.Contains("2"))
    itype = 2;
  else if(fin.Contains("4"))
    itype = 4;
  else{
    printf("wrong fin! %s\n", fin.Data());
  }

  plot(itype);
  return 0;
}
